package com.project2.controller;


import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.ResultVo;
import com.project2.bean.po.MarketUser;
import com.project2.service.WxCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {



    @Autowired
    WxCouponService wxCouponService;

    /**
     * 展示用户可以领取的优惠卷
     * @param pageBo 分页信息
     * @param request 请求用于得到用户id
     * @author Frank Zhu
     */
    @RequestMapping("list")
    public ResultVo getCouponList(PageBo pageBo, HttpServletRequest request){
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser userInfo = (MarketUser) request.getSession().getAttribute(header);
        Integer userId = userInfo.getId();

        CommonData couponList = wxCouponService.list(pageBo, userId);
        return ResultVo.ok(couponList);
    }

    /**
     * 用户获取优惠卷
     * @param request 请求，为了得到用户的id
     * @param map json语句，只有couponId
     * @author Frank Zhu
     */
    @RequestMapping("receive")
    public ResultVo userReceiveCoupon(HttpServletRequest request, @RequestBody Map map){
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser userInfo = (MarketUser) request.getSession().getAttribute(header);
        Integer userId = userInfo.getId();

        Integer couponId = (Integer) map.get("couponId");

        // Boolean getOrNot = wxCouponService.userGetCoupon(couponId, userId);
        Boolean getOrNot = wxCouponService.receive(couponId, userId);

        if(!getOrNot){
            return ResultVo.ok("优惠卷不可领取!", 401);
        }

        return ResultVo.ok();
    }

    /**
     * 获取用户的优惠卷list
     * @param request 拿用户id
     * @param status 优惠卷状态
     * @param pageBo 分页信息
     * @author Frank Zhu
     */
    @RequestMapping("mylist")
    public ResultVo usersCouponList(HttpServletRequest request, Short status, PageBo pageBo){
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser userInfo = (MarketUser) request.getSession().getAttribute(header);
        Integer userId = userInfo.getId();

        CommonData data = wxCouponService.mylist(userId, status, pageBo);

        return ResultVo.ok(data);
    }

    @RequestMapping("selectlist")
    public ResultVo couponListThatUserCanUse(Integer cartId,HttpServletRequest request){
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser userInfo = (MarketUser) request.getSession().getAttribute(header);
        CommonData commonData = wxCouponService.selectCouponThatUserCanUse(cartId,userInfo);
        return ResultVo.ok(commonData);
    }

    @RequestMapping("exchange")
    public ResultVo getCouponByExchangeCode(@RequestBody Map map, HttpServletRequest request){
        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser userInfo = (MarketUser) request.getSession().getAttribute(header);
        Integer userId = userInfo.getId();

        String code = (String) map.get("code");

        Boolean done = wxCouponService.exchange(userId, code);

        if(!done){
            return ResultVo.ok("优惠卷不可领取!", 401);
        }

        return ResultVo.ok();
    }
}
